﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Write.API.Application.Commands.DoctorCommand;
using MediatR;

namespace D.Medicalcare.Write.API.Application.Handlers.DoctorHarder
{
    public class DelDoctorHarder : IRequestHandler<DelDoctorCommand, APIResult<int>>
    {

        private readonly IBaseRepository<Doctor> baseRepository;
        private readonly ILogger<DelDoctorHarder> logger;

        public DelDoctorHarder(IBaseRepository<Doctor> baseRepository, ILogger<DelDoctorHarder> logger)
        {
            this.baseRepository = baseRepository;
            this.logger = logger;
        }

        public async Task<APIResult<int>> Handle(DelDoctorCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> result = new APIResult<int>();
                var xi = baseRepository.GetAll().Where(x => x.DoctorId == request.DoctorId).First();
                xi.IsDeleted = true;
                var a = baseRepository.Update(xi).Result;
                if (a > 0)
                {
                    result.Code = APIEnum.Success;
                    result.Msg = "删除成功";
                    result.Data = a;
                    logger.LogInformation("删除医生成功");

                }
                else
                {
                    result.Code = APIEnum.Failed;
                    result.Msg = "删除失败";
                    result.Data = a;
                    logger.LogError("删除医生失败");

                }

                return await Task.FromResult(result);
            }
            catch (Exception ex)
            {
                logger.LogError("删除医生异常" + ex.Message);
                throw;
            }
        }
    }
}
